package com.ruoyi.cust.controller;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cust.domain.CityProject;
import com.ruoyi.cust.service.ICityProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目Controller
 *
 * @author ruoyi
 * @date 2024-12-06
 */
@Api("项目")
@RestController
@RequestMapping("/city/project")
public class CityProjectController extends BaseController {
    @Autowired
    private ICityProjectService cityProjectService;

    /**
     * 查询项目列表
     */
    @ApiOperation("查询项目列表")
    @PreAuthorize("@ss.hasPermi('city:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(CityProject cityProject) {
        startPage();
        List<CityProject> list = cityProjectService.selectJoinList(cityProject);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @ApiOperation("导出项目列表")
    @PreAuthorize("@ss.hasPermi('city:project:export')")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CityProject cityProject) {
        List<CityProject> list = cityProjectService.selectJoinList(cityProject);
        ExcelUtil<CityProject> util = new ExcelUtil<CityProject>(CityProject.class);
        util.exportExcel(response, list, "项目数据");
    }

    /**
     * 获取项目详细信息
     */
    @ApiOperation("获取项目详细信息")
    @PreAuthorize("@ss.hasPermi('city:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId) {
        return success(cityProjectService.selectJoinById(projectId));
    }

    /**
     * 新增项目
     */
    @ApiOperation("新增项目")
    @PreAuthorize("@ss.hasPermi('city:project:add')")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CityProject cityProject) {
        cityProject.setProjectId(Long.valueOf(IdUtil.getSnowflake(1, 1).nextId()));
        return toAjax(cityProjectService.insertCityProject(cityProject));
    }

    /**
     * 修改项目
     */
    @ApiOperation("修改项目")
    @PreAuthorize("@ss.hasPermi('city:project:edit')")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CityProject cityProject) {
        return toAjax(cityProjectService.updateCityProject(cityProject));
    }

    /**
     * 删除项目
     */
    @ApiOperation("删除项目")
    @PreAuthorize("@ss.hasPermi('city:project:remove')")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds) {
        return toAjax(cityProjectService.deleteCityProjectByProjectIds(projectIds));
    }
}
